import { imagesCodeAPI, loginAPI } from '@/api'
// 引入token_js文件
import { setToken, getToken, getId, setId, removeId, removeToken } from '@/plugins/local_info.js'
export default {
  namespaced: true,
  state: {
    dkd_token: getToken() || '', // token
    imgUrl: '',
    dkd_userId: getId() || 0, // 用户id
    userInfo: {}
  },
  mutations: {
    // 验证码图片
    GET_CODE(state, imgUrl) {
      state.imgUrl = imgUrl
    },
    // 登录
    LOGIN_INFO(state, data) {
      state.dkd_token = data.token
      state.dkd_userId = data.userId
      state.userInfo = data
      setToken(data.token)
      setId(data.userId)
    },
    LOGOUT(state) {
      state.dkd_token = ''
      state.dkd_userId = ''
      removeToken()
      removeId()
    }
  },
  actions: {
    // 验证码图片
    async getImagesCode(context, codeNum) {
      try {
        const { config: { url: imgUrl } } = await imagesCodeAPI(codeNum)
        context.commit('GET_CODE', imgUrl)
        return Promise.resolve('获取成功')
      } catch (error) {
        return Promise.reject(error)
      }
    },
    // 登录接口
    async getLogin(context, userInfo) {
      try {
        const { data } = await loginAPI(userInfo)
        context.commit('LOGIN_INFO', data)
        // console.log(data)
        return Promise.resolve(data)
      } catch (error) {
        return Promise.reject(error)
      }
    }

  },
  getters: {

  }
}
